/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef USART_HEADER
#define USART_HEADER

#define USART_RXBUFSIZE 256
#define USART_TXBUFSIZE 64

#define USART_CTS_MAX (USART_RXBUFSIZE * 0.75) 
#define USART_CTS_MIN (USART_RXBUFSIZE * 0.66)

struct usart_txbuf {
	uint8_t buf[USART_TXBUFSIZE];
	uint16_t head;
	uint16_t tail;
	uint16_t used;

	uint8_t inuse;
	uint8_t echo;
};

struct usart_rxbuf {
	uint8_t buf[USART_RXBUFSIZE];
	uint16_t head;
	uint16_t tail;
	uint16_t used;

	uint8_t inuse;
	uint8_t echo;
};

void usart_init(void);
uint16_t usart_tx(uint8_t, uint8_t *, uint16_t);
uint16_t usart_rx(uint8_t, uint8_t *, uint16_t);
uint16_t buf_cat(uint8_t, uint8_t *, uint16_t);
uint16_t buf_rem(uint8_t, uint8_t *, uint16_t);
void usart_puts(uint8_t, const char *);
void usart_put(uint8_t, const void *, uint16_t);
int usart_getc(uint8_t);
void usart_putc(uint8_t, int);
int8_t usart_peek(uint8_t, char);
void usart_flush(uint8_t);
uint16_t usart_rxused(uint8_t);
void usart_echo(uint8_t, uint8_t);
uint8_t userland_wait(void);

#endif
